﻿#include "Server.h"

Server::Server(QWidget* parent)
	:QWidget(parent)
{
	init();
}
Server::~Server()
{
	m_webSocketServer->close();
	delete m_webSocketServer;


}

void Server::init()
{
	start(QHostAddress::Any, 8888);

	layoutinit();
}

bool Server::start(const QHostAddress& address, quint16 port)
{
	//如果还不存在server，就创建一个：
	if (!m_webSocketServer)
	{
		m_webSocketServer = new QWebSocketServer("Remote_file_transfer", QWebSocketServer::SslMode::NonSecureMode	/*加密通信*/,this);
		connect(m_webSocketServer, &QWebSocketServer::newConnection, this, &Server::onNewConnection);
	}
	return m_webSocketServer->listen(address,port);
}


void Server::onNewConnection()
{
	//如果服务器有连接：
	while (m_webSocketServer->hasPendingConnections())
	{
		//判断下一个连接：
		auto client = m_webSocketServer->nextPendingConnection();
			//连接槽函数：
		connect(client, &QWebSocket::disconnected, this, &Server::onDisConnecttion);
		connect(client, &QWebSocket::binaryMessageReceived, this, &Server::onBinaryMessageRecived);
		connect(client, &QWebSocket::binaryMessageReceived, this, [=]
			{
				QString newdata = "\t\tthe signal of binaryMessageReceived  triggering";
				emit data(newdata);
			});
		connect(client, &QWebSocket::textMessageReceived, this, &Server::onTextMessageRecived);
		connect(client, &QWebSocket::textMessageReceived, this, [=]
			{
				QString newdata = "\t\tthe signal of textMessageReceived triggering";
				emit data(newdata);
			});

		//把client转换成键：key
		auto key = QString::number((quint16)client);
		//给hash容器 插入数据：
		m_websocketclient.insert(key, client);
		qDebug() << "have new connection > " << m_websocketclient.size();

		QString newdata = "\tthere are the devices connected to the server";
		emit data(newdata);
	}
}

void Server::onDisConnecttion()
{
	auto client = dynamic_cast<QWebSocket*>(sender());
	if(client)
	{
		m_websocketclient.remove(QString::number((quint16)client));
		qDebug() << "dis connection > " << m_websocketclient.size();
	}

	QString newdata = "\ta device has been disconnected from this server";
	emit data(newdata);
}

void Server::onBinaryMessageRecived(const QByteArray& data)
{
	qDebug() << "onBinaryMessageRecived ";
	auto client = dynamic_cast<QWebSocket*> (sender());
	if(client)
	{
		for (auto c : m_websocketclient)
		{
			//不要自己发给自己
			//if(c != client)
				c->sendBinaryMessage(data);
		}
	}
}

void Server::onTextMessageRecived(const QString& data)
{
	qDebug() << "onTextMessageRecived ";
	auto client = dynamic_cast<QWebSocket*> (sender());
	if(client)
	{
		for (auto c : m_websocketclient)
		{
			//不要自己发给自己
			//if (c != client)
				c->sendTextMessage(data);
		}
	}
}





void Server::layoutinit()
{
	this->resize(640, 480);
	this->setWindowTitle("webServer");
	this->setWindowIcon(QIcon("./images/server.ico"));


	m_listview = new QListView(this);
	m_listview->resize(640, 480);
	m_listModel = new QStandardItemModel(this);

	m_listview->setModel(m_listModel);
	m_listview->setEditTriggers(QListView::NoEditTriggers);
	m_listview->setSelectionBehavior(QListView::SelectionBehavior::SelectRows);

	UpdateView();

	QObject::connect(this, &Server::data, this, &Server::UpdateView);
}
void Server::UpdateView(QString connectdata)
{
	QStandardItem* item = new QStandardItem(connectdata);
	m_listModel->appendRow(item);


	//跳转到该视图的最下面，数据在随时刷新，确保可以随时看到最新信息：
	m_listview->scrollToBottom();

}


